// service统一出口
import ZxRequest from './axios.js';
import { ElMessage } from 'element-plus';
import { Local } from '@/utils/storage';
//import refresh from './refresh/refresh'
const zxRequest = new ZxRequest({
	baseURL: import.meta.env.VITE_API_URL,
	timeout: 50000,
	interceptors: {
		requestInterceptor: async (config) => {
			// console.log('请求成功拦截-实例')
			// 登录添加验证码头部信息
			if (config.url === '/admin/passport/login') {
				let captchaId = Local.get('captchaId');
				config.headers.common['captchaId'] = captchaId;
			}
			// 验证token
			let token = Local.get('token');
			if (token && token.access_token) {
				config.headers.common['Authorization'] = token.access_token;
			}
			return config;
		},
		requestInterceptorCatch: (err) => {
			// console.log('请求失败拦截-实例')
			return err;
		},
		responseInterceptor: (res) => {
			// console.log('响应成功拦截-实例')
			if (res.code === -1) {
				ElMessage({
					message: '登录信息已过期,请重新登录!',
					type: 'error',
				});
				return;
			}
			return res;
		},
		responseInterceptorCatch: (err) => {
			// console.log('响应失败拦截-实例')
			alert('ssssefe');
			return err;
		},
	},
});

export default zxRequest;
